package com.knife.easyexcel.write.utils;

import com.knife.easyexcel.write.entity.ColorCell;
import com.knife.easyexcel.write.handler.ColorCellWriteHandler;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * 创建{@link com.knife.easyexcel.write.entity.ColorCell}的工具类
 */
public class ColorCellUtil {

    /**
     * 添加自定义颜色列
     *
     * @param colorColumnList  excel行的列
     * @param columnName 导出的列名
     * @param colors     颜色
     */
    public static void addColorCellColumn(List<ColorCell.ColorColumn> colorColumnList, String columnName, IndexedColors colors) {
        ColorCell.ColorColumn colorColumn = new ColorCell.ColorColumn(columnName, colors);
        colorColumnList.add(colorColumn);
    }

    /**
     * 创建颜色单元格写入工具
     *
     * @param colorColumnList 单元格颜色
     * @param dataList 数据
     * @param needHandleRowFunction 提供需要处理的行
     * @param <T> excel写入数据泛型
     * @return 单元格颜色写入处理器
     */
    public static <T> ColorCellWriteHandler buildColorCellWriteHandler(List<ColorCell.ColorColumn> colorColumnList, List<T> dataList, Function<T, Boolean> needHandleRowFunction) {
        List<ColorCell> colorCells = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            T data = dataList.get(i);
            if (needHandleRowFunction.apply(data)) {
                ColorCell colorCell = new ColorCell(i + 1, colorColumnList);
                colorCells.add(colorCell);
            }
        }
        return new ColorCellWriteHandler(colorCells);
    }

}
